home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Macintosh Tracker 1.1 Source / Tracker Server Folder / getopt.c < prev    next >
Text File  |  1993-05-18  |  2KB  |  99 lines

  1. /* getopt.c */
  2.  
  3. /* $Id: getopt.c,v 1.3 1993/01/26 14:10:38 espie Exp espie $
  4.  * $Log: getopt.c,v $
  5.  * Revision 1.3  1993/01/26  14:10:38  espie
  6.  * Fixe
  7.  * Fixed up stupdi end of file bug.
  8.  *
  9.  * Revision 1.2  1992/11/27  10:29:00  espie
  10.  * General cleanup
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <ctype.h>
  16.  
  17. #include "defs.h"
  18. #include "getopt.h"
  19.  
  20. int optind = 1;
  21. char *optarg = 0;
  22. static not_an_option = 0;
  23.  
  24. LOCAL int parse_option(argv, option)
  25. char *argv[];
  26. struct long_option *option;
  27.     {
  28.     optind++;
  29.     if (option->argn)
  30.         optarg = argv[optind++];
  31.     return option->abbrev;
  32.     }
  33.  
  34. int getopt(argc, argv, options)
  35. int argc;
  36. char *argv[];
  37. struct long_option *options;
  38.     {
  39.     if (not_an_option == optind)
  40.         return -1;
  41.     if (optind >= argc)
  42.         return -1;
  43.     if (argv[optind][0] == '-')
  44.         {
  45.         char *match = argv[optind]+1;
  46.         if (strlen(match) == 1)
  47.             {
  48.             if (match[0] == '-')
  49.                 {
  50.                 not_an_option = ++optind;
  51.                 return -1;
  52.                 }
  53.             while(options->fulltext)
  54.                 {
  55.                 if (options->abbrev == match[0])
  56.                     return parse_option(argv, options);
  57.                 else
  58.                     options++;
  59.                 }
  60.             return -1;
  61.             }
  62.         else
  63.             {
  64.             int max_match = 0;
  65.             struct long_option *best = 0;
  66.  
  67.             while (options->fulltext)
  68.                 {
  69.                 int i;
  70.                 for (i = 0; ; i++)
  71.                     {
  72.                     if (options->fulltext[i] == 0 && match[i] == 0)
  73.                         return parse_option(argv, options);
  74.                     if (match[i] == 0)
  75.                         {
  76.                         if (i > max_match)
  77.                             {
  78.                             max_match = i;
  79.                             best = options;
  80.                             }
  81.                         break;
  82.                         }
  83.                     if (tolower(options->fulltext[i]) != tolower(match[i]))
  84.                         break;
  85.                     }
  86.                 options++;
  87.                 }
  88.             if (max_match < 3)
  89.                 {
  90.                 fprintf(stderr, "Unrecognized option: %s\n", match);
  91.                 return -1;
  92.                 }
  93.             return parse_option(argv, best);
  94.             }
  95.         }
  96.     else
  97.         return -1;
  98.     }
  99.